var inp1 = document.getElementsByClassName("inp1")[0];
var floag = true;
function user() {
  var reg = /[a-zA-Z][a-zA-Z0-9]{3,15}$/g;
  floag = true;
  if (inp1.value == "") {
    alert("*请输入用户名");
    floag = false;
    return floag;
  } else if (reg.test(inp1.value)) {
    return floag;
  } else {
    alert("请输入以字母和数字组成的用户名");
    floag = false;
    return floag;
  }
}
var inp2 = document.getElementsByClassName("inp2")[0];
function password() {
  var reg = /[a-zA-Z][0-9]{3,9}$/g;
  floag = true;
  if (inp2.value == "") {
    alert("密码不能为空");
    floag = false;
    return floag;
  } else if (reg.test(inp2.value)) {
    return floag;
  } else {
    alert("请输入由字母和数字组成的密码");
    floag = false;
    return floag;
  }
}
var btn = document.querySelector('.btn');
btn.onclick = function () {
  if (user() && password()) {
    var xhr = new XMLHttpRequest()
    obj.open("post", "http://192.168.199.101:8080/BaorSystem/user/login")
    xhr.setRequestHeader("content-type", "application/json");
    var daet = {
      userAccount: inp1.value,
      userPassword: inp2.value
    }
    xhr.send(JSON.stringify(daet));
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        var data = JSON.parse(xhr.responseText);
        console.log(data);
        if (data.code == 200) {
          alert('登陆成功');
          localStorage.setItem("userName", inp1.value);
          localStorage.setItem("userName", inp1.value);
          location.href = '';
        }
      }
    }

  }
}
